/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
Ext.onReady(function() {
    Ext.QuickTips.init(); // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';
    var store = new Ext.data.SimpleStore({
        fields: ['cid', 'cv'],
        data: [['4', '90-100%'], ['3', '70-90%'], ['2', '50-70%'], ['1', '<50%']]
    });
    var id = Ext.urlDecode(window.location.search.substring(1))['assignment_id'];
    var paper_id = Ext.urlDecode(window.location.search.substring(1))['paper_id'];
    var edit = Ext.urlDecode(window.location.search.substring(1))['edit'];
    var review_id = 0;
    var ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../review/get'
        }),
        reader: new Ext.data.JsonReader({
            root: 'reviews',
            totalProperty: 'totalCount',
            id: 'id'
        },
        [{
            name: 'id'
        },
        {
            name: 'grade'
        },
        {
            name: 'comment'
        },
        {
            name: 'assignment_id'
        },
        {
            name: 'date_reviewed',
            type: 'date',
            dateFormat: 'Y-d-m G:i:s'
        },
        {
            name: 'theory_grade'
        },
        {
            name: 'practice_grade'
        },
        {
            name: 'overview_grade'
        },
        {
            name: 'conf_comment'
        },
        {
            name: 'competency'
        }])
    });
    var paper_store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../review/getpaper'
        }),
        reader: new Ext.data.JsonReader({
            root: 'papers',
            totalProperty: 'totalCount',
            id: 'id'
        },
        [{
            name: 'id'
        },
        {
            name: 'title'
        },
        {
            name: 'abstract'
        },
        {
            name: 'file_pdf'
        },
        {
            name: 'file_doc'
        },
        {
            name: 'area_name_' + lang
        },
        {
            name: 'category_name_' + lang
        }])
    });
    var download_pdf;
    var download_doc;
    var ct = new Ext.Panel({
        renderTo: 'paper-details',
        frame: true,
        title: lbl_panel_title,
        width: 580,
        height: 200,
        layout: 'border',
        items: [{
            id: 'detailPanel',
            region: 'center',
            bodyStyle: {
                background: '#ffffff',
                padding: '7px'
            },
            html: ''
        }]
    });
    var reviewForm = new Ext.FormPanel({
        frame: true,
        title: lbl_review_paper,
        bodyStyle: 'padding:5px 5px 0',
        labelAlign: 'top',
        width: 580,
        waitMsgTarget: true,
        items: [new Ext.form.FieldSet({
            title: lbl_newresult,
            autoHeight: true,
            layout: 'column',
            items: [{
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                allowBlank: false,
                items: [{
                    fieldLabel: grade_desc[5],
                    name: 'theory_grade',
                    id: 'theory_grade5',
                    inputType: 'radio',
                    value: '5'
                }]
            },
            {
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[4],
                    name: 'theory_grade',
                    id: 'theory_grade4',
                    inputType: 'radio',
                    value: '4'
                }]
            },
            {
                columnWidth: .15,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[3],
                    name: 'theory_grade',
                    id: 'theory_grade3',
                    inputType: 'radio',
                    value: '3'
                }]
            },
            {
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[2],
                    name: 'theory_grade',
                    id: 'theory_grade2',
                    inputType: 'radio',
                    value: '2'
                }]
            },
            {
                columnWidth: .15,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[1],
                    name: 'theory_grade',
                    id: 'theory_grade1',
                    inputType: 'radio',
                    allowBlank: false,
                    value: '1'
                }]
            },
            {
                columnWidth: .22,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[0],
                    name: 'theory_grade',
                    id: 'theory_grade0',
                    inputType: 'radio',
                    value: '0'
                }]
            }]
        }), new Ext.form.FieldSet({
            title: lbl_application,
            autoHeight: true,
            layout: 'column',
            items: [{
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                allowBlank: false,
                items: [{
                    fieldLabel: grade_desc[5],
                    name: 'practice_grade',
                    id: 'practice_grade5',
                    inputType: 'radio',
                    value: '5'
                }]
            },
            {
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[4],
                    name: 'practice_grade',
                    id: 'practice_grade4',
                    inputType: 'radio',
                    value: '4'
                }]
            },
            {
                columnWidth: .15,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[3],
                    name: 'practice_grade',
                    id: 'practice_grade3',
                    inputType: 'radio',
                    value: '3'
                }]
            },
            {
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[2],
                    name: 'practice_grade',
                    id: 'practice_grade2',
                    inputType: 'radio',
                    value: '2'
                }]
            },
            {
                columnWidth: .15,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[1],
                    name: 'practice_grade',
                    id: 'practice_grade1',
                    inputType: 'radio',
                    value: '1'
                }]
            },
            {
                columnWidth: .22,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[0],
                    name: 'practice_grade',
                    id: 'practice_grade0',
                    inputType: 'radio',
                    value: '0'
                }]
            }]
        }), new Ext.form.FieldSet({
            title: lbl_overview,
            autoHeight: true,
            layout: 'column',
            items: [{
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                allowBlank: false,
                items: [{
                    fieldLabel: grade_desc[5],
                    name: 'overview_grade',
                    id: 'overview_grade5',
                    inputType: 'radio',
                    value: '5'
                }]
            },
            {
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[4],
                    name: 'overview_grade',
                    id: 'overview_grade4',
                    inputType: 'radio',
                    value: '4'
                }]
            },
            {
                columnWidth: .15,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[3],
                    name: 'overview_grade',
                    id: 'overview_grade3',
                    inputType: 'radio',
                    value: '3'
                }]
            },
            {
                columnWidth: .16,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[2],
                    name: 'overview_grade',
                    id: 'overview_grade2',
                    inputType: 'radio',
                    value: '2'
                }]
            },
            {
                columnWidth: .15,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[1],
                    name: 'overview_grade',
                    id: 'overview_grade1',
                    inputType: 'radio',
                    value: '1'
                }]
            },
            {
                columnWidth: .22,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: grade_desc[0],
                    name: 'overview_grade',
                    id: 'overview_grade0',
                    inputType: 'radio',
                    value: '0'
                }]
            }]
        }), new Ext.form.FieldSet({
            title: lbl_general,
            autoHeight: true,
            layout: 'column',
            allowBlank: false,
            items: [{
                columnWidth: .10,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                allowBlank: false,
                blankText: msg_blank,
                items: [{
                    fieldLabel: o_grade_desc[4],
                    name: 'grade',
                    id: 'grade4',
                    inputType: 'radio',
                    value: '4'
                }]
            },
            {
                columnWidth: .10,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: o_grade_desc[3],
                    name: 'grade',
                    id: 'grade3',
                    inputType: 'radio',
                    value: '3'
                }]
            },
            {
                columnWidth: .30,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: o_grade_desc[2],
                    name: 'grade',
                    id: 'grade2',
                    inputType: 'radio',
                    value: '2'
                }]
            },
            {
                columnWidth: .30,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: o_grade_desc[1],
                    name: 'grade',
                    id: 'grade1',
                    inputType: 'radio',
                    value: '1'
                }]
            },
            {
                columnWidth: .20,
                layout: 'form',
                border: false,
                defaultType: 'textfield',
                items: [{
                    fieldLabel: o_grade_desc[0],
                    name: 'grade',
                    id: 'grade0',
                    inputType: 'radio',
                    value: '0'
                }]
            }]
        }), new Ext.form.TextArea({
            fieldLabel: lbl_conf_comment,
            name: 'conf_comment',
            id: 'conf_comment',
            width: '400px',
            allowBlank: false,
            blankText: msg_blank
        }), new Ext.form.TextArea({
            fieldLabel: lbl_scientific_comment,
            name: 'comment',
            id: 'comment',
            width: '400px',
            allowBlank: false,
            blankText: msg_blank
        }), new Ext.form.ComboBox({
            fieldLabel: lbl_competency,
            hiddenName: 'competency',
            id: 'cid',
            store: store,
            valueField: 'cid',
            displayField: 'cv',
            typeAhead: true,
            mode: 'local',
            triggerAction: 'all',
            emptyText: lbl_select,
            editable: false,
            selectOnFocus: true,
            allowBlank: false,
            blankText: msg_blank,
            width: 190
        })],
        buttons: [{
            text: lbl_submit,
            type: 'submit',
            name: 'review',
            handler: function() {
                var form = reviewForm.getForm();
                var missing = '';
                var tg = form.getValues()['theory_grade'];
                var pg = form.getValues()['practice_grade'];
                var og = form.getValues()['overview_grade'];
                var g = form.getValues()['grade'];
                var flag = false;
                if (!tg) {
                    missing += lbl_newreview;
                    flag = true;
                }
                if (!pg) {
                    if (flag) missing += ', ';
                    missing += lbl_application;
                    flag = true;
                }
                if (!og) {
                    if (flag) missing += ', ';
                    missing += lbl_overview;
                    flag = true;
                }
                if (!g) {
                    if (flag) missing += ', ';
                    missing += lbl_overview;
                    flag = true;
                }
                if (flag) {
                    Ext.Msg.show({
                        title: lbl_incomplete,
                        msg: lbl_please + missing,
                        buttons: Ext.Msg.OK,
                        icon: Ext.MessageBox.WARNING
                    });
                    return;
                }
                var p = {
                        assignment_id: id,
                        id: review_id
                    };
   
                if (form.isValid()) {
                    form.submit({
                        url: 'write',
                        params: p,
                        waitMsg: lbl_saving,
                        success: function(result) {
                            window.location = 'assigned';
                        }
                    });
                }
            }
        }]
    });
    var loaded = function(r, options, success) {
        if (r.length > 0) {
            reviewForm.getForm().setValues([{
                id: 'comment',
                value: r[0].data['comment']
            },
            {
                id: 'conf_comment',
                value: r[0].data['conf_comment']
            }]);
            document.getElementById('theory_grade' + r[0].data.theory_grade).checked = true;
            document.getElementById('practice_grade' + r[0].data.practice_grade).checked = true;
            document.getElementById('overview_grade' + r[0].data.overview_grade).checked = true;
            document.getElementById('grade' + r[0].data.grade).checked = true;
            Ext.getCmp('cid').setValue(r[0].data.competency);
            review_id = r[0].id;
        }
    };
    var loaded_paper = function(r, options, success) {
        if (r) {
            var detailPanel = Ext.getCmp('detailPanel');
            if (r[0].data.file_pdf != '') {
                download_pdf = '<a href="../paper/download/?id={id}&ft=pdf"><img src="../../images/pdf.png"></a><a href="../paper/download/?id={id}&ft=pdf">{file_pdf}</a>&nbsp;';
            } else {
                download_pdf = '';
            }
            if (r[0].data.file_doc != '') {
                download_doc = '<a href="../paper/download/?id={id}&ft=doc"><img src="../../images/doc.png"></a><a href="../paper/download/?id={id}&ft=doc">{file_doc}</a>';
            } else {
                download_doc = '';
            }
            var paperTplMarkup = ['<table>', '<tr><td valign="top"><b>' + lbl_title + ':</b></td><td>{title}</td></tr>', '<tr><td valign="top"><b>' + lbl_download + ':</b></td><td>' + download_pdf + download_doc + '</td></tr>', '<tr><td valign="top"><b>' + lbl_area + ':</b></td><td>{area_name_' + lang + '}</td></tr>', '<tr><td valign="top"><b>' + lbl_category + ':</b></td><td>{category_name_' + lang + '}</td></tr>', '<tr><td valign="top"><b>' + lbl_abstract + ':</b></td><td>{abstract}</td></tr>', '</table>'];
            var paperTpl = new Ext.Template(paperTplMarkup);
            paperTpl.overwrite(detailPanel.body, r[0].data);
        }
    };
    if (true) {
        ds.load({
            params: {
                assignment_id: id
            },
            callback: loaded
        });
    }
    paper_store.load({
        params: {
            paper_id: paper_id
        },
        callback: loaded_paper
    });
    reviewForm.render('review-form');
});
